package com.jdy.haoduoaiteacher.util;

import android.content.Context;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;

/**
 * DateUtils for message date.
 * 
 * @author zp @ 2015-6-8 下午2:42:55
 */

public class DateUtils {

  public static CharSequence getTimestampString(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MMMdd日 HH:mm");
    return sdf.format(date);
  }

  public static String toTimeBySecond(int length) {

    return null;
  }

  public static String convert2ShortFormat(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      Date tmpDate = sdf.parse(date);
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      return formatter.format(tmpDate);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
  }

  public static String getNowDateString() {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date = df.format(Calendar.getInstance().getTime());
    return date;
  }

  public static Date toDate(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      return sdf.parse(date);
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
  }

  public static boolean isCloseEnough(long currentMsgTime, long lastMsgTime) {

    if ((currentMsgTime - lastMsgTime) / 60000 < 5)
      return true;
    return false;
  }

  public static boolean isCloseEnough(String currentMsgTime, String lastMsgTime) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Date currentDate;
    try {
      currentDate = sdf.parse(currentMsgTime);

      Date lastDate = sdf.parse(lastMsgTime);

      long timeGap = currentDate.getTime() - lastDate.getTime();

      if (timeGap / 60000 < 5)
        return true;
      return false;
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return false;
    }

  }

  public static String getTime(int reslen) {
    int min = reslen / 60;
    int s = reslen % 60;
    String ss;
    if (s < 10)
      ss = "0" + s;
    else
      ss = String.valueOf(s);
    return min + ":" + ss;
  }

  public static String getTime2(int reslen) {
    int min = reslen / 60;
    int s = reslen % 60;
    String ss;
    if (s < 10)
      ss = "0" + s;
    else
      ss = String.valueOf(s);
    return min + "分" + ss + "秒";
  }

  public static void descSort(ArrayList<HashMap<String, Object>> mList,
      final String order) {
    Collections.sort(mList, new Comparator<HashMap<String, Object>>() {
      @Override
      public int compare(HashMap<String, Object> arg0,
          HashMap<String, Object> arg1) {
        return Integer.parseInt(arg0.get(order).toString())
            - Integer.parseInt(arg1.get(order).toString());
      }
    });
  }

  public static void ascendSort(ArrayList<HashMap<String, Object>> mList,
      final String order) {
    Collections.sort(mList, new Comparator<HashMap<String, Object>>() {
      @Override
      public int compare(HashMap<String, Object> arg0,
          HashMap<String, Object> arg1) {
        return Integer.parseInt(arg1.get(order).toString())
            - Integer.parseInt(arg0.get(order).toString());
      }
    });
  }

  public static String getStringDateShort(String time) {
    Date currentTime = new Date(Long.parseLong(time) * 1000);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(currentTime);
    return dateString;
  }

  public static void showToast(Context context, String result) {
    Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
  }

  public static String getDate(int days) {
    int year = days / 365;
    int month = days % 365 / 30;
    if (year > 0) {
      if (month > 0)
        return year + "岁" + month + "月";
      else
        return year + "岁";
    } else {
      return month + "月";
    }
  }

  public static String humanReadableByteCount(long bytes, boolean si) {
    int unit = si ? 1000 : 1024;
    if (bytes < unit)
      return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
  }
}
